package com.itheima.health.utils;

import com.itheima.health.common.CookieConstant;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
    public static void setCookie(HttpServletResponse response,Integer cookieValue) {
        Cookie cookie = new Cookie(CookieConstant.USER_ID, cookieValue.toString());
        cookie.setPath("/");
        cookie.setMaxAge(CookieConstant.USER_ID_TIMEOUT);
        response.addCookie(cookie);
    }

    public static String parseCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (CookieConstant.USER_ID.equals(cookie.getName())) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

}
